/* ----------------------------------------------------------------------
*
* Copyright (C) 2025 XiaoFei. All rights reserved.
*
* @file:		XiaoFei_Usart.c
* @author:		XiaoFei
* @Date:     	18-May-2025
* @Revision: 	V1
* @Project: 	XiaoFei BSP Library
*
* -------------------------------------------------------------------- */

#include "stm32f10x.h"
#include "XiaoFei_Delay.h"
#include "XiaoFei_Usart.h"
#include "stdio.h"
#include "stdarg.h"

USARTBand_Typedef Band_Use;

/* 
* @function: 	Usart_Init
* @parameter:	[in] Band					 The bandrate you need.
*/
void Usart_Init(USARTBand_Typedef Band)
{
	Usart_TX_RCC(1);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = Usart_TX_Pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(Usart_TX_GPIO, &GPIO_InitStructure);
	Band_Use=Band;
}

/* 
* @function: 	Usart_SendByte
* @parameter:	[in] data					 The byte will be send.
*/
void Usart_SendByte(unsigned char data) 
{
	Usart_TX(0);
	XiaoFei_Delay_us(Band_Use);
	for(unsigned char i = 0; i < 8; i++) 
	{
		Usart_TX(data&0x01);
		XiaoFei_Delay_us(Band_Use);
		data >>= 1;
	}
	Usart_TX(1);
	XiaoFei_Delay_us(Band_Use);
}

/* 
* @function: 	Usart_SendString
* @parameter:	[in] String					 The string will be send.
*/
void Usart_SendString(char *String)
{
	for (unsigned int i = 0; String[i] != '\0'; i++)
		Usart_SendByte(String[i]);
}

/* 
* @function: 	Usart_Printf
* @parameter:	[in] format					 The parameter will be send in Usart_Printf.
*/
void Usart_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg, format);
	vsprintf(String, format, arg);
	va_end(arg);
	Usart_SendString(String);
}


